﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Bling.DSL;
using Bling.Properties;
using Bling.Animation;
using Bling.WPF;
using Bling.Slides;
using Bling.Util;
using Bling.Linq;
using Bling.Physics;
using Bling.Lighting;
using Bling.D3D;

namespace Bling.LightingTutorial {
  public class Driver : SlideHolder {
    public Driver() {
      this["Title"] = () => new Title();
      this["Point light"] = () => new PointLightTest();
      this["Spot light"] = () => new SpotLightTest();
      this["Direction light"] = () => new DirectionLightTest();
      Init();
    }
    [STAThread]
    public static void Main(string[] args) {
      var win = new Window() { Title = "Bling Physics Tutorial", Content = new Driver() };
      new Application().Run(win);
      return;
    }
  }
  public class Title : Canvas {

    public static readonly ImageSource TitleNormal = Properties.Resources.title_normal.LoadBitmap();
    PointLightBl light0;

    public Title() {
 //     this.Bl().Background = Brushes.MidnightBlue;

      var normalMap = new ImageBrush() { ImageSource = TitleNormal }.Bl();

      var slider = new SliderBl(this) { Visibility = Visibility.Hidden, Width = this.Width, Minimum = 0, Maximum = 1 };
      var thumb = new ThumbBl(this) { CanDrag = true, LeftTop = this.Bl().CenterSize + (180 + (slider.Value)*180).ToDegrees().ToPoint() * 400.0, Background = Colors.White, ZIndex = 100, };
      light0 = new PointLightBl() { Position = new Point3DBl(thumb.LeftTop, 150), Color = slider.Value.Lerp(Colors.White, Colors.Orange), Attenuation = { Constant = 5e-3, Linear = 3e-4, Quadratic = 4e-5 } };
      slider.Value.Animate.Forever().AutoReverse().Duration(2000).To = slider.Maximum;

      var eyepos = new Point3DBl(this.Bl().CenterSize, 100);

      var newCanvasb = new CanvasBl(this) { CenterPosition = this.Bl().CenterSize, Size = this.Bl().Size, Background = Brushes.Transparent, ZIndex = 5 };
      newCanvasb.Effect.Custom = (input, uv) => {
        return LightingTechniques.DiffuseColor(uv, Colors.White, light0, new PointBl(0, 0), this.Bl().Size) + Colors.Navy;
      };

      var newCanvas = new CanvasBl(this) { CenterPosition = this.Bl().CenterSize, Size = TitleNormal.Size(), Background = Brushes.Transparent, ZIndex = 10 };
      newCanvas.Effect.Custom = (input, uv) => {
        var color = LightingTechniques.DiffuseSpecularColor(uv, Colors.Yellow, normalMap, light0, eyepos, newCanvas.LeftTop, newCanvas.Size, 1, 0.65).ScRGB;
       return ColorBl.FromScRGB(normalMap[uv].ScA, normalMap[uv].ScA*(color + Colors.Yellow.Bl().ScRGB * 0.3));
      };   
    }
  }
}